package com.spring.oaassist.web.service.impl;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import jxl.*;
import net.sf.ehcache.store.MemoryStore;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import sun.misc.BASE64Decoder;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.oa.api.web.dao.OaMapper;
import com.spera.aliyunContent.AliyunContent;
import com.spera.aliyunUtil.CreateOSSClient;
import com.spera.aliyunUtil.SperaOSSUtil;
//import com.spera.genutil.TokenUtil;
import com.spera.shopoa.util.ObjectUtils;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spera.shopoa.util.ShopOaSessionUtil.OaUser;
import com.spring.common.web.model.TracelogboWithBLOBs;
import com.spring.oaassist.web.controller.BusinessController;
import com.spring.oaassist.web.dao.OaAssistMapper;
import com.spring.oaassist.web.model.Custom;
import com.spring.oaassist.web.requestModel.CourseTextbook;
import com.spring.oaassist.web.requestModel.RequestUser;
import com.spring.oaassist.web.service.OaAssistService;
import com.spring.oaassist.web.service.ReportService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.util.UDate;
import com.spring.security.core.util.UpdateMap;
import com.spring.security.web.dao.CommonMapper;
import com.spring.security.web.dao.JedisCache;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;
import com.sso.api.web.dao.SsoMapper;
import com.sso.api.web.dao.SsoTracelogboMapper;
import com.sso.api.web.model.Family;
import com.sso.api.web.model.MemberCard;
import com.sso.api.web.model.MemberInfo;
import com.sso.api.web.model.Users;

@Service
public class OaAssistServiceImpl implements OaAssistService {
    @Resource
    private OaAssistMapper oaAssistMapper;    
    
    @Resource
	private CommonMapper commonMapper;
    
    @Resource
	private SsoMapper ssoMapper;
    
    @Resource
	private OaMapper oaMapper;
    
	@Resource
	private JedisCache jedisCache;
	
	@Resource private SsoTracelogboMapper ssoTracelogboMapper;
	
	@Resource
	private ReportService reportService;

	@Override
	public List<Map<String, Object>> queryScheduler(Map<String, Object> m) {
		return oaAssistMapper.queryScheduler(m);
	}
    
	@Override
	public List<Map<String, Object>> queryCustomerList(Map<String, Object> m) {
		
		List<Map<String, Object>> retL = ssoMapper.queryCustomerList(m);
		return retL;
	}
	
	@Override
	public List<MemberInfo> checkMemberList(List<Map<String, Object>> retL) {
		List<MemberInfo> retM = Lists.newArrayList();
		StringBuffer userIds = new StringBuffer();
		for(Map<String, Object> ml:retL){
			if(ApplicationUtils.isNotNull(ApplicationUtils.Object2String(ml.get("cUserID")))){
				userIds.append("'"+ApplicationUtils.Object2String(ml.get("cUserID"))+"',");
			}
		}
		String strUserIds = "";
		if(ApplicationUtils.isNotNull(userIds.toString())){
			strUserIds = userIds.toString().substring(0,userIds.toString().length()-1);
			//SSO查找用户及监护人等信息
			Map<String,Object> parm = Maps.newHashMap();
			parm.put("userIds", strUserIds);
			retM = ssoMapper.queryUserMemberInfo(parm);
			if(retM!=null&&retM.size()>0){
				for(MemberInfo mm:retM){
					jedisCache.hset("memberinfo_rom".getBytes(), mm.getCid().getBytes(),ObjectUtils.serialize(mm));
				}
			}
		}
		
		List<MemberInfo> retObj = Lists.newArrayList();
		for(Map<String, Object> ml:retL){
			MemberInfo mm = (MemberInfo)ObjectUtils.unserialize(jedisCache.hget("memberinfo_rom".getBytes(), ApplicationUtils.Object2String(ml.get("cUserID")).getBytes()));
			if(mm!=null/*&&ApplicationUtils.isNotNull(mm.getMemberNumber())*/){
				List<Map<String,Object>> rm = oaAssistMapper.queryMemberPackageCourse(mm.getMemberNumber());
				if(rm!=null&&rm.size()>0){
					mm.setTb_num(ApplicationUtils.Object2String(rm.get(0).get("tb")));
					mm.setZj_num(ApplicationUtils.Object2String(rm.get(0).get("zj")));

				}
			}else{
				mm = new MemberInfo();
			}
			mm.setMid(ApplicationUtils.Object2String(ml.get("cUserID")));
			mm.setMname((StringUtils.isEmpty(mm.getCname())?ApplicationUtils.Object2String(ml.get("userName")):mm.getCname()));
			mm.setC_family_id(ApplicationUtils.Object2String(ml.get("c_family_id")));
			retObj.add(mm);
		}
		
		return retObj;
	}
	
	@Override
	public List<Map<String, Object>> checkCustomerServerList(List<Map<String, Object>> retL) {
		List<MemberInfo> retM = Lists.newArrayList();
		StringBuffer userIds = new StringBuffer();
		for(Map<String, Object> ml:retL){
			if(ApplicationUtils.isNotNull(ApplicationUtils.Object2String(ml.get("c_user_id")))){
				userIds.append("'"+ApplicationUtils.Object2String(ml.get("c_user_id"))+"',");
			}
		}
		String strUserIds = "";
		if(ApplicationUtils.isNotNull(userIds.toString())){
			strUserIds = userIds.toString().substring(0,userIds.toString().length()-1);
			//SSO查找用户及监护人等信息
			Map<String,Object> parm = Maps.newHashMap();
			parm.put("userIds", strUserIds);
			retM = ssoMapper.queryUserMemberInfo(parm);
			if(retM!=null&&retM.size()>0){
				for(MemberInfo mm:retM){
					jedisCache.hset("memberinfo_rom".getBytes(), mm.getCid().getBytes(),ObjectUtils.serialize(mm));
				}
			}
		}
		
		for(int i=0;i<retL.size();i++){
			Map<String, Object> ml = retL.get(i);
			MemberInfo mm = (MemberInfo)ObjectUtils.unserialize(jedisCache.hget("memberinfo_rom".getBytes(), ApplicationUtils.Object2String(ml.get("c_user_id")).getBytes()));
			if(mm!=null){
				retL.get(i).put("memberType", mm.getMemberType());
				retL.get(i).put("member_number", mm.getMemberNumber());
				retL.get(i).put("baby_name", mm.getCname());
			}
			

		}
		return retL;
	}

	@Override
	public List<Map<String, Object>> queryCustomerServerList(
			Map<String, Object> parm) {
		return oaAssistMapper.queryCustomerServerList(parm);
	}

	@Override
	public List<Map<String, Object>> queryCustomerSpace(Map<String, Object> parm) {
		return oaAssistMapper.queryCustomerSpace(parm);
	}

	@Override
	public List<Map<String, Object>> queryClassCommont(Map<String, Object> parm) {
		return oaAssistMapper.queryClassCommont(parm);
	}

	@Override
	public List<Map<String, Object>> queryPhotoList(Map<String, Object> parm) {
		return oaAssistMapper.queryPhotoList(parm);
	}

	@Override
	public List<Map<String, Object>> queryVideoList(Map<String, Object> parm) {
		return oaAssistMapper.queryVideoList(parm);
	}

	@Override
	public List<Map<String, Object>> queryTextList(Map<String, Object> parm) {
		return oaAssistMapper.queryTextList(parm);
	}

	@Override
	public List<Map<String, Object>> queryCommentList(Map<String, Object> parm) {
		return oaAssistMapper.queryCommentList(parm);
	}

	@Override
	public List<Map<String, Object>> queryCourseInfoList(
			Map<String, Object> parm) {
		return oaAssistMapper.queryCourseInfoList(parm);
	}

	@Override
	public List<Map<String, Object>> queryCourseStudentsList(
			Map<String, Object> parm) {
		return oaAssistMapper.queryCourseStudentsList(parm);
	}

	@Override
	public List<Map<String, Object>> queryCourseScheduler(
			Map<String, Object> parm) {
		return oaAssistMapper.queryCourseScheduler(parm);
	}

	@Override
	public List<Map<String, Object>> queryMemberConsumeHis(
			Map<String, Object> parm) {
		return oaAssistMapper.queryMemberConsumeHis(parm);
	}

	@Override
	public List<Map<String, Object>> queryUnMemberConsumeHis(
			Map<String, Object> parm) {
		return oaAssistMapper.queryUnMemberConsumeHis(parm);
	}

	@Override
	public List<Map<String, Object>> queryUserInfo(Map<String, Object> parm) {
		return ssoMapper.queryUserInfo(parm);
	}

	@Override
	public List<Map<String, Object>> queryBizCueByApplication(
			Map<String, Object> parm) {
		return oaAssistMapper.queryBizCueByApplication(parm);
	}

	@Override
	public List<Map<String, Object>> queryMenuZtree(Map<String, Object> parm) {
		return oaMapper.queryMenuZtree(parm);
	}

	@Override
	public List<Map<String, Object>> queryRoleList(Map<String, Object> parm) {
		return oaMapper.queryRoleList(parm);
	}

	@Override
	public int saveRoleMenu(String roleid,String menuids) {
		Map<String, Object> parm = new HashMap<String, Object>();
		parm.put("user_priv_id", roleid);
		oaMapper.deleteRoleMenu(parm);
		for(String func_id_str:menuids.split(",")){
			parm.put("func_id_str", func_id_str);
			oaMapper.saveRoleMenu(parm);
		}
		return 1;
	}

	@Override
	public int saveMenu(String id, String pId, String name, String func_url,
			String func_order,String is_show) {
		Map<String, Object> parm = new HashMap<String, Object>();
		parm.put("pid", ApplicationUtils.isNotNull(pId)?pId:"0");
		parm.put("func_name", name);
		parm.put("func_url", func_url);
		parm.put("func_order", ApplicationUtils.isNotNull(func_order)?func_order:"0");
		parm.put("is_show", is_show);
		int i=0;
		if(ApplicationUtils.isNotNull(id)){
			parm.put("id",id);
			i = oaMapper.updateMenu(parm);
		}else{
			i = oaMapper.insertMenu(parm);
		}
		return i;
	}

	@Override
	public int deleteMenu(String id) {
		int i = oaMapper.deleteMenu(id);
		if(i>0){
			List<String> mid = new ArrayList<String>();
			mid.add(id);
			deleteMenuByPids(mid);
		}
		return i;
	}
	
	/**
	 * 级联删除
	 * @param ids
	 */
	private void deleteMenuByPids(List<String> ids){
		List<String> cmenu = new ArrayList<String>();
		if(ApplicationUtils.isNotNull(ids)){
			for(String id:ids){
				List<String> cids = oaMapper.queryMenuByPid(id);
				if(ApplicationUtils.isNotNull(cids)){
					for(String cid:cids){
						cmenu.add(cid);
					}
				}
				oaMapper.deleteMenu(id);
			}
			if(cmenu.size()>0){
				deleteMenuByPids(cmenu);
			}
		}
	}

	/**
	 * 查询商品。并根据乐园将该乐园中是否存在该商品的状态返回
	 */
	@Override
	public List<Map<String, Object>> queryProduct(Map<String, Object> parm) {
		return oaAssistMapper.queryProduct(parm);
	}
	


	/**
	 * 保存乐园商品
	 */
	@Override
	public void saveParkProduct(List<Map<String, Object>> checkedArr) {
		Map<String,Object> parm = new HashMap<String,Object>();
		parm = checkedArr.get(0);
		String parkid = ApplicationUtils.Object2String(parm.get("parkid"));
		if(ApplicationUtils.isNotNull(parkid)){

			checkedArr.remove(0);//移除第一行参数列
			for(Map<String,Object> model:checkedArr){
					parm = new HashMap<String,Object>();
					parm.put("id", ApplicationUtils.randomUUID());
					parm.put("unitPrice", ApplicationUtils.Object2String(model.get("parkPrice")));
					parm.put("kids_parkID",parkid);
					parm.put("isDiscount",ApplicationUtils.Object2String(model.get("isDiscount")));
					parm.put("order_productID", ApplicationUtils.Object2String(model.get("product_id")));
					UpdateMap up = new UpdateMap("order_product_kids_park");
					up.addWhere("kids_parkID", parkid);
					up.addWhere("order_productID", ApplicationUtils.Object2String(model.get("product_id")));
					List<Map<String,Object>> lm = commonMapper.select(up);
					if(lm==null||lm.size()==0){
						oaAssistMapper.insertParkProduct(parm);
					}else{
						updateParkProduct(parkid,ApplicationUtils.Object2String(model.get("product_id")),ApplicationUtils.Object2String(model.get("parkPrice")),
								ApplicationUtils.Object2String(model.get("isDiscount")));
					}
			}
		}
	}
	
	@Override
	public void removeParkProduct(String park_id,String product_id){
		UpdateMap up = new UpdateMap("order_product_kids_park");
		up.addField("EntityAvailableState","20");
		up.addWhere("kids_parkID", park_id);
		up.addWhere("order_productID", product_id);
		commonMapper.update(up);
	}
	
	private void updateParkProduct(String kids_parkID,String order_productID,String unitPrice,String isDiscount){
		UpdateMap up = new UpdateMap("order_product_kids_park");
		up.addField("EntityAvailableState","10");
		up.addField("unitPrice",unitPrice);
		up.addField("isDiscount",StringUtils.isEmpty(isDiscount)?"0":isDiscount);
		up.addWhere("order_productID", order_productID);
		up.addWhere("kids_parkID", kids_parkID);
		commonMapper.update(up);
	}

	@Override
	public String queryKidsParkByOaShopId(String shop_dept_id) {
		List<Map<String, Object>> retm = oaAssistMapper.queryKidsParkByOaShopId(shop_dept_id);
		String kids_parkID = "";
		if(retm!=null&&retm.size()>0){
			kids_parkID = ApplicationUtils.Object2String(retm.get(0).get("id"));
		}
		return kids_parkID;
	}

	@Override
	public List<Map<String, Object>> queryAssistUserInfo(
			Map<String, Object> parm) {
		return oaAssistMapper.queryAssistUserInfo(parm);
	}

	@Override
	public List<Map<String, Object>> querySsoUserInfo(Map<String, Object> parm) {
		return ssoMapper.queryUserInfo(parm);
	}

	@Override
	public String saveAssistUserInfo(String assistantId, String assistantName,
			List<Map<String, Object>> checkedArr) {
		String ret = "";
		Map<String,Object> qm = new HashMap<String,Object>();
		for(Map<String, Object> model:checkedArr){
			if(StringUtils.isEmpty(model.get("phone"))){
				continue;
			}
			qm.put("ins_phone", model.get("phone"));
			List<Map<String,Object>> list = oaAssistMapper.queryAssistUserInfo(qm);
			if(list!=null&&list.size()>0){
				ret+=model.get("phone")+",";
				continue;
			}else{
				model.put("id", ApplicationUtils.randomUUID());
				model.put("assistantId", assistantId);
				model.put("assistantName", assistantName);
				oaAssistMapper.saveAssistUserInfo(model);
			}
		}
		return StringUtils.isEmpty(ret)?"":"手机号为"+ret+"的用户已有辅导员";
	}

	@Override
	public void delAssistUserInfo(String c_user_id) {
		oaAssistMapper.delAssistUserInfo(c_user_id);
	}
	
	
	@Override
	public List<Custom> getCustomList(Map<String, Object> parm) {
		return oaAssistMapper.getCustomList(parm);
	}
	
	@Override
	public List<Users> getUserByIDs(Map<String, Object> parm) {
		return null;
				//ssoMapper.getUserByIDS(parm);
	}
	
	@Override
	public List<Family> queryFamilyInfoByUserId(Map<String, Object> parm) {
		return ssoMapper.queryFamilyInfoByUserId(parm);
	}
	
	@Override
	public List<MemberCard> queryMemberCardInfo(Map<String, Object> parm) {
		return oaAssistMapper.queryMemberCardInfo(parm);
	}

	@Override
	public int updateWorkSchedule(Map<String, Object> data) {
		return oaAssistMapper.updateWorkSchedule(data);
	}

	@Override
	public int saveWorkSchedule(Map<String, Object> data) {
		return oaAssistMapper.saveWorkSchedule(data);
	}
	
	
	@Override
	public int deleteScheduleByID(String scheduleID) {
		return oaAssistMapper.deleteScheduleByID(scheduleID);
	}

	@Override
	public List<Map<String, Object>> queryCustCourseApply(
			Map<String, Object> parm) {
		return oaAssistMapper.queryCustCourseApply(parm);
	}
	
	

	@Override
	public Map<String, Object> reComment(String parentID, String toUserID, String toUserName, String toUserType,
    		String fromUserID, String fromUserName, String fromUserType, String serviceID, String businessTypeCode, 
    		String serviceModuleCode, String comment) throws Exception {
		Map<String, Object> m = new HashMap();

        Map<String, Object> parm = new HashMap<String, Object>();

        
    	parm.put("parentID", parentID);
    	parm.put("toUserID", toUserID);
    	parm.put("toUserName",toUserName);
    	parm.put("toUserType",toUserType);
    	parm.put("fromUserID",fromUserID);
    	parm.put("fromUserName",fromUserName);
    	parm.put("fromUserType",fromUserType);
    	parm.put("serviceID",serviceID);
    	parm.put("businessTypeCode",businessTypeCode);
    	parm.put("serviceModuleCode",serviceModuleCode);
    	parm.put("comment",comment);
				
		int effect = oaAssistMapper.reComment(parm);
    	
		if(effect==0){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "评论失败");

			return m;
		}

		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "评论成功");
		return m;
	}
	
	
	

	@Override
	public Map<String, Object> commentPhone(String comment, String course_image_id, String c_user_id, 
			String user_name) throws Exception {
		Map<String, Object> m = new HashMap();

        Map<String, Object> parm = new HashMap<String, Object>();

        
    	parm.put("comment", comment);
    	parm.put("course_image_id", course_image_id);
    	parm.put("c_user_id",c_user_id);
    	parm.put("user_name",user_name);
			
		int effect = oaAssistMapper.commentPhone(parm);
    	
		if(effect==0){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "评论失败");

			return m;
		}

		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "评论成功");
		return m;
	}
	
	
	@Override
	public  List<Map<String, Object>> getPhoneComment(String course_image_id) throws Exception {
		Map<String, Object> m = new HashMap();

        Map<String, Object> parm = new HashMap<String, Object>();

        List<Map<String, Object>> list = oaAssistMapper.getPhoneComment(course_image_id);    

		return list;
	}
	
	
	
	@Override
	public  List<Map<String, Object>> getVideoComment(String videoID) throws Exception {
		Map<String, Object> m = new HashMap();

        Map<String, Object> parm = new HashMap<String, Object>();

        List<Map<String, Object>> list = oaAssistMapper.getVideoComment(videoID)  ; 

		return list;
	}
	
	
	
	
	@Override
	public Map<String, Object> commentVideo(String comment, String videoID, String t_user_id) throws Exception {
		Map<String, Object> m = new HashMap();

        Map<String, Object> parm = new HashMap<String, Object>();

        
    	parm.put("comment", comment);
    	parm.put("videoID", videoID);
    	parm.put("t_user_id",t_user_id);
			
		int effect = oaAssistMapper.commentVideo(parm);
    	
		if(effect==0){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "评论失败");

			return m;
		}

		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "评论成功");
		return m;
	}
	
	@Override
	public List<HashMap<String, Object>> getUsersList(String c_user_id,
			String phone, String real_name, String identity_card)
			throws Exception {

		Map<String, Object> parm = new HashMap<String, Object>();
		parm.put("c_user_id", c_user_id);
		parm.put("phone", phone);
		parm.put("real_name", real_name);
		parm.put("identity_card", identity_card);

		List<HashMap<String, Object>> list = ssoMapper.getUsersList(parm);
		
		return list;
	}
	
	
	@Override
	public HashMap<String, Object> getBabyList(String skip,
			String rows, String real_name,String phone) throws Exception {
		
		HashMap<String, Object> m=new HashMap<String, Object>();
		

		if(skip==null||skip.equals("")){
			skip="0";
			rows="100000";
		}
		
		Map<String, Object> parm =MyTools.pageIndexToSkip(null, null, skip, rows);
		
		
		parm.put("real_name", real_name);
		parm.put("phone", phone);

		List<HashMap<String, Object>> list = ssoMapper.getBabyList(parm);
		
		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");
		
		return m;

	}

	
	@Override
	public Map<String, Object> addPhoto(String formImgData, String c_user_id,HttpServletRequest request) throws Exception {
		Map<String, Object> m = new HashMap<String, Object>();

        Map<String, Object> parm = new HashMap<String, Object>();
        
        MyTools.checkIsNotNull(formImgData,c_user_id);
        
        String[] strlist=formImgData.split(",");
        
        if(strlist.length<2){
    		m.put(ApplicationUtils.STATUS, "0");
    		m.put(ApplicationUtils.MSG, "请输入正确格式图片参数");
    		return m;
        }

       @SuppressWarnings("restriction")
       byte[] buffer = new BASE64Decoder().decodeBuffer(strlist[1]);
        
        ByteArrayInputStream bais=new ByteArrayInputStream(buffer);
        
        String photo_src=this.imageFileUpOss(AliyunContent.fileBucket, "folder"+"/" + UDate.getDate() + "/"+ ApplicationUtils.randomUUID()+".jpg", bais,(long)buffer.length);
        
        System.out.println(photo_src);
        
        parm.put("c_user_id", c_user_id);
        parm.put("photo_src", photo_src);
        
        int effect=ssoMapper.updateUserPhoto(parm);
        
        if(effect<1){
    		m.put(ApplicationUtils.STATUS, "0");
    		m.put(ApplicationUtils.MSG, "更新失败");
    		return m;
        }
        
        TracelogboWithBLOBs traclelogba = MyTools.getOaLogByRequest(request);
		traclelogba.setEntitytypename("t_teacher");
		traclelogba.setEntityid(c_user_id);
		traclelogba.setLogcategory(1);
		traclelogba.setLogdescription(MyTools.getChangeByMap(parm, "修改"));
		int effect2 = ssoTracelogboMapper.insert(traclelogba);
		System.out.println(effect2);
        
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "更新成功");
		return m;
	}
	
    public String imageFileUpOss(String bucketName, String key, InputStream fis,Long fileSize)
            throws Exception {
    	OSSClient client = CreateOSSClient.getInstance().getOSSClient();
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(fileSize);
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");
       
        client.putObject(bucketName, key, fis, objectMeta);
        return "http://" + bucketName + ".img-cn-hangzhou.aliyuncs.com/" + key;
    }
    
    
    
	@Override
	public List<Map<String, Object>> getAdmissionWay() throws Exception {
		
		List<Map<String, Object>> list = oaAssistMapper.getAdmissionWay();
	
		return list;

	}
	
	//easyUIDemo
	@Override
	public List<Map<String, Object>> queryCourseTextbook(Map<String, Object> parm) {
		return oaAssistMapper.queryCourseTextbook(parm);
	}
	
	@Override
	public BaseResult deleteCourseTextbook(String ids) throws Exception {
		Map<String, Object> parm =new HashMap<String, Object>();
		
		String[] idList=ids.split(",");		
		parm.put("ids", idList);		
		int effect=oaAssistMapper.deleteCourseTextbook(parm);		
		if(effect<1){
			return new BaseResult("0","删除失败");
		}		
		return new BaseResult("1","删除成功");
	}

	@Override
	public BaseResult saveOrUpdateCourseTextbook(CourseTextbook ct) throws Exception {
		Map<String, Object> parm =new HashMap<String, Object>();
		
		int effect=0;
		
		if(MyTools.checkIsNull(ct.getId())){
			effect=oaAssistMapper.insertCourseTextbook(ct);
		}else{
			effect=oaAssistMapper.updateCourseTextbook(ct);
		}		
	
		if(effect<1){
			return new BaseResult("0","更新成功");
		}		
		return new BaseResult("1","更新失败");
	}

	@Override
	public BaseResult addUsers(MultipartFile file,HttpServletRequest request) throws Exception {
		Map<String, Object> parm =new HashMap<String, Object>();
		
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		
        Workbook workbook = null;
        Cell cell = null;
        
        workbook = Workbook.getWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheet(0);

        String failBaby="";
        String exist="";
        int success=0;
        
        for(int i=2; i<sheet.getRows(); i++){
        	        	        	
        	RequestUser user=new RequestUser();
        	
        	String babyName = sheet.getCell(0,i).getContents();  
        	
        	if(babyName==null||babyName.equals("")){
        		continue;
        	}
        	
        	String memberName = sheet.getCell(1,i).getContents();  
        	String sex = sheet.getCell(2,i).getContents();   
        	
        	String sexCode="1";
        	
        	if(sex.equals("女")){
        		sexCode="2";
        	}
        	
        	String birthday = sheet.getCell(3,i).getContents();   
        	
        	//DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        	//Date birthdayDate = dateFormat.parse(birthday);
        	
        	//long birthdaytime=birthdayDate.getTime()/1000;
        	
        	String infoSource = sheet.getCell(5,i).getContents(); 
        	
        	String infoSourceCode=oaAssistMapper.getInfoSource(infoSource);
        	
        	String customeAssist = sheet.getCell(6,i).getContents();         	
        	String customeAssistID=oaMapper.getUserIDByName(customeAssist);
        	
        	String teacherName = sheet.getCell(7,i).getContents();
           	String t_user_id=oaMapper.getUserIDByName(teacherName);
        	
        	
        	String parentName = sheet.getCell(8,i).getContents();   
        	String parentMemberName = sheet.getCell(9,i).getContents();   
        	String phone = sheet.getCell(10,i).getContents();   
        	String identityCard  = sheet.getCell(11,i).getContents();    
        	String work  = sheet.getCell(12,i).getContents();    
        	String address  = sheet.getCell(13,i).getContents();         	
        	String desc  = sheet.getCell(13,i).getContents();   	
        	
        	user.setReal_name(babyName);
        	user.setMember_name(memberName);
        	user.setSex_code(sexCode);
        	user.setParentName(parentName);
        	user.setParentMemberName(parentMemberName);
        	user.setBirthday(birthday);
        	user.setT_user_id(t_user_id);
        	user.setT_username(teacherName);
        	user.setAdmission_way(infoSourceCode);
        	user.setPhone(phone);
        	user.setIdentity_card(identityCard);
        	user.setWork(work);
        	user.setAddress(address);
        	user.setAdmission_note(desc);
        	//Map<String, Object> map=(Map<String, Object>)businessController.addUserSubmit(user, request,null) ;

			user.setKids_parkID(oaUser.getKids_park_id());
			user.setT_user_id(oaUser.getOa_user_id());
			user.setT_username(oaUser.getOa_user_name());
			user.setC_role_id("0");	
			
        	Map<String, Object> map=reportService.addUsers(user);
        	
        	if(map.get(ApplicationUtils.STATUS).equals("0")){
        		//return new BaseResult("0",(String)map.get(ApplicationUtils.MSG));
        		if(failBaby.length()==0){
        			failBaby=babyName;
        		}else{
            		failBaby=failBaby+","+babyName;
        		}
        	}
        	else if(map.get(ApplicationUtils.STATUS).equals("2")){
        		if(exist.length()==0){
        			exist=babyName;
        		}else{
        			exist=exist+","+babyName;
        		}        		
        	}else{
        		success++;
        	}
        	        	
        }
        String str="成功导入"+success+"条记录";
        if(failBaby.length()>0){
        	str=str+","+failBaby+"导入失败";
        }
        if(exist.length()>0){
        	str=str+","+exist+"手机号已存在";
        }
     
        return new BaseResult("1",str);
        
	}	

	@Override
	public BaseResult copyWorkSchedule(String fromTime,String toTime) throws Exception {
		Map<String, Object> parm =new HashMap<String, Object>();		
		parm.put("fromTime", fromTime);
		parm.put("toTime", toTime);
		int	effect=oaAssistMapper.copyWorkSchedule(parm);		
		return new BaseResult("1","更新成功");
	}
	
	public BaseResult updateUser(String cid,String nickname,String englishName,HttpServletRequest request) throws Exception{
		Map<String, Object> parm =new HashMap<String, Object>();		
		parm.put("cUserId", cid);
		parm.put("nickname", nickname);
		parm.put("englishName", englishName);

		int effect=ssoMapper.updateUser(parm);
		
		TracelogboWithBLOBs tl = MyTools.getOaLogByRequest(request);
		tl.setEntitytypename("c_family_member");
		tl.setEntityid(cid);
		tl.setLogcategory(2);
		tl.setLogdescription(MyTools.getChangeByMap(parm, "修改"));
		int effect2=ssoTracelogboMapper.insert(tl);
		System.out.println(effect2);
		
		return MyTools.checkResult(effect);
	}
}
